package com.wang.tool.util;

import com.wang.tool.monitor.LinkLogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;

/**
 * @Description 简单日志 一般提供给组件类使用
 * 业务类使用LinkLogUtil
 * @Author shaopeng
 * @Date 2022/11/18
 * @Version 1.0
 * @see LinkLogUtil
 */
public class SimpleLogUtil {

    /**
     * biz日志
     */
    public static final Logger BIZ_LOGGER = LoggerFactory.getLogger("S_BIZ");


    public static void debug(String format, Object... args) {
        if (!BIZ_LOGGER.isDebugEnabled() || format == null) {
            return;
        }
        try {
            BIZ_LOGGER.debug(MessageFormat.format(format, args));
        } catch (Throwable e) {
            BIZ_LOGGER.error("SimpleLogUtilDebugError", e);
        }
    }

    public static void debug(Logger logger, String format, Object... args) {
        if (logger == null || !logger.isDebugEnabled() || format == null) {
            return;
        }
        try {
            logger.debug(MessageFormat.format(format, args));
        } catch (Throwable e) {
            BIZ_LOGGER.error("SimpleLogUtilDebugError", e);
        }
    }


    public static void info(String format, Object... args) {
        if (!BIZ_LOGGER.isInfoEnabled() || format == null) {
            return;
        }
        try {
            BIZ_LOGGER.info(MessageFormat.format(format, args));
        } catch (Throwable e) {
            BIZ_LOGGER.error("SimpleLogUtilInfoError", e);
        }
    }

    public static void info(Logger logger, String format, Object... args) {
        if (logger == null || !logger.isInfoEnabled() || format == null) {
            return;
        }
        try {
            logger.info(MessageFormat.format(format, args));
        } catch (Throwable e) {
            BIZ_LOGGER.error("SimpleLogUtilInfoError", e);
        }
    }


    public static void warn(String format, Object... args) {
        if (!BIZ_LOGGER.isWarnEnabled() || format == null) {
            return;
        }
        try {
            BIZ_LOGGER.warn(MessageFormat.format(format, args));
        } catch (Throwable e) {
            BIZ_LOGGER.error("SimpleLogUtilWarnError", e);
        }
    }

    public static void warn(Logger logger, String format, Object... args) {
        if (logger == null || !logger.isWarnEnabled() || format == null) {
            return;
        }
        try {
            logger.warn(MessageFormat.format(format, args));
        } catch (Throwable e) {
            BIZ_LOGGER.error("SimpleLogUtilWarnError", e);
        }
    }


    public static void error(String code, Throwable e, String format, Object... args) {
        try {
            BIZ_LOGGER.error("[" + (code == null ? LinkLogUtil.EMPTY_PLACEHOLDER : code) + "]" + (format == null ? "" : MessageFormat.format(format, args)), e);
        } catch (Throwable ex) {
            BIZ_LOGGER.error("SimpleLogUtilErrorError", ex);
        }
    }


    public static void error(String code, Logger logger, Throwable e, String format, Object... args) {
        try {
            logger.error("[" + (code == null ? LinkLogUtil.EMPTY_PLACEHOLDER : code) + "]" + (format == null ? "" : MessageFormat.format(format, args)), e);
        } catch (Throwable ex) {
            BIZ_LOGGER.error("SimpleLogUtilErrorError", ex);
        }
    }


    public static void error(String code, Throwable e) {
        try {
            BIZ_LOGGER.error("[" + (code == null ? LinkLogUtil.EMPTY_PLACEHOLDER : code) + "]", e);
        } catch (Throwable ex) {
            BIZ_LOGGER.error("SimpleLogUtilErrorError", ex);
        }
    }


    public static void error(String code, Logger logger, Throwable e) {
        try {
            logger.error("[" + (code == null ? LinkLogUtil.EMPTY_PLACEHOLDER : code) + "]", e);
        } catch (Throwable ex) {
            BIZ_LOGGER.error("SimpleLogUtilErrorError", ex);
        }
    }


}
